package com.airili.wiki.service.impl;

import com.airili.wiki.domain.Category;
import com.airili.wiki.domain.CategoryExample;
import com.airili.wiki.mapper.CategoryMapper;
import com.airili.wiki.req.CategoryQueryReq;
import com.airili.wiki.req.CategorySaveReq;
import com.airili.wiki.resp.CategoryQueryResp;
import com.airili.wiki.resp.PageResp;
import com.airili.wiki.service.CategoryService;
import com.airili.wiki.util.CopyUtil;
import com.airili.wiki.util.SnowFlake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description:
 * @author: Airili
 * @create: 2021-09-24 10:12
 **/
@Service
public class CategoryServiceImpl implements CategoryService {
    private static final Logger LOG = LoggerFactory.getLogger(CategoryServiceImpl.class);

    @Resource
    private SnowFlake snowFlake;

    @Resource
    private CategoryMapper categoryMapper;

    @Override
    public PageResp<CategoryQueryResp> list(CategoryQueryReq req) {
        // 以下两行代码为 Example 通用写法格式
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();

        // 根据名字模糊查询，动态SQL
//        if (!ObjectUtils.isEmpty(req.getName())) {
//            criteria.andNameLike("%" + req.getName() + "%");
//        }

        // 使用 pagehelper 分页插件，
        // 分页功能 = 查总数 (select count(0) from table) + 查当前页数据 (select xxx from table limit ?)
        // 使用时注意！！！ ： 该分页只对首个遇到的 select 起作用，所以使用的时候应该把该语句放在查询语句的前一行
        PageHelper.startPage(req.getPage(), req.getSize());
        List<Category> categorys = categoryMapper.selectByExample(categoryExample);

        // 使用 pageinfo
        PageInfo<Category> pageInfo = new PageInfo<>(categorys);
        LOG.info("总行数: {}", pageInfo.getTotal());
        LOG.info("总页数: {}", pageInfo.getPages());

        List<CategoryQueryResp> categoryQueryResps = CopyUtil.copyList(categorys, CategoryQueryResp.class);

        PageResp<CategoryQueryResp> resp = new PageResp<>();
        resp.setTotal(pageInfo.getTotal());
        resp.setList(categoryQueryResps);
        return resp;
    }

    @Override
    public List<CategoryQueryResp> all(CategoryQueryReq req) {
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.setOrderByClause("sort asc");
        List<Category> categorys = categoryMapper.selectByExample(categoryExample);
        List<CategoryQueryResp> categoryQueryResps = CopyUtil.copyList(categorys, CategoryQueryResp.class);
        return categoryQueryResps;
    }

    @Override
    public void save(CategorySaveReq req) {
        Category category = CopyUtil.copy(req, Category.class);
        if (ObjectUtils.isEmpty(req.getId())) {
            // 新增
            category.setId(snowFlake.nextId());
            categoryMapper.insert(category);
        } else {
            // 更新
            categoryMapper.updateByPrimaryKey(category);
        }
    }

    @Override
    public void delete(Long id) {
        categoryMapper.deleteByPrimaryKey(id);
    }

}
